package com.sanyue.codingsettlement.strategy;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sanyue.codingsettlement.common.ErrorCode;
import com.sanyue.codingsettlement.exception.BusinessException;
import com.sanyue.codingsettlement.model.dto.question.QuestionContent;
import com.sanyue.codingsettlement.model.dto.question.QuestionContentOption;
import com.sanyue.codingsettlement.model.entity.App;
import com.sanyue.codingsettlement.model.entity.Question;
import com.sanyue.codingsettlement.model.entity.ScoringResult;
import com.sanyue.codingsettlement.model.entity.UserAnswer;
import com.sanyue.codingsettlement.service.AppService;
import com.sanyue.codingsettlement.service.QuestionService;
import com.sanyue.codingsettlement.service.ScoringResultService;

import javax.annotation.Resource;
import java.util.List;

/**
 * 自定义得分判题策略
 * @author: Guan
 **/
@ScoringStrategyConfig(appType = 0,scoringStrategy = 0)
public class CustomScoreScoringStrategy implements ScoringStrategy{

    @Resource
    private AppService appService;

    @Resource
    private QuestionService questionService;


    @Resource
    private ScoringResultService scoringResultService;

    @Resource
    private ScoringStrategyExecutor scoringStrategyExecutor;

    @Override
    public UserAnswer doScore(App app, List<String> choiceList) {
        if(app == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"应用不存在");
        }
        // 2. 根据appId获取题目和评分结果
        QueryWrapper<Question> questionQueryWrapper = new QueryWrapper<>();
        questionQueryWrapper.eq("appId",app.getId());
        Question question = questionService.getOne(questionQueryWrapper);
        if(question == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"题目不存在");
        }

        //   a. 首先将题目转换为List
        List<QuestionContent> questionContentList = JSONUtil.toList(question.getQuestionContent(), QuestionContent.class);
        //   b. 判断题目和答案集合是否为空（字符串判断 集合默认有一个 "[]，永远不为空"）
        if(CollUtil.isEmpty(questionContentList) || CollUtil.isEmpty(choiceList)){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"题目或答案为空");
        }
        //   c. 如果题目和答案的数量都不一样，直接报错
        if(questionContentList.size() != choiceList.size()){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"题目和答案数量不一致");
        }
        // 3. 进行判题
        //   a. 依次获取每个选项的分数, 得到评分结果
        int score = 0;
        for(int i = 0;i < choiceList.size();i++){
            for (QuestionContentOption questionContentOption : questionContentList.get(i).getOptions()) {
                if(questionContentOption.getKey().equals(choiceList.get(i))){
                    score += questionContentOption.getScore();
                    continue;
                }
            }
        }
        // 4. 根据分数，进行得分结果的判断
        QueryWrapper<ScoringResult> scoringResultQueryWrapper = new QueryWrapper<>();
        // 得到比 score 分数小的第一个评分结果
        scoringResultQueryWrapper = scoringResultQueryWrapper
                .eq("appId", app.getId())
                .le("resultScoreRange",score)
                .orderByDesc("resultScoreRange");
        ScoringResult scoringResult = scoringResultService.getOne(scoringResultQueryWrapper);
        // 5. 返回UserAnswer
        UserAnswer userAnswer = new UserAnswer();
        if(scoringResult != null){
            userAnswer.setResultId(scoringResult.getId());
            userAnswer.setResultName(scoringResult.getResultName());
            userAnswer.setResultDesc(scoringResult.getResultDesc());
            userAnswer.setResultPicture(scoringResult.getResultPicture());
        }
        userAnswer.setResultScore(score);
        return userAnswer;
    }
}
